package com.replay;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.FutureListener;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * description
 *
 * @author zk
 * @since 2018/9/17
 */
public class testRelpay {

    @Test
    public void testReplayServer() throws InterruptedException {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
        ChannelFuture channelFuture = serverBootstrap.channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_RCVBUF, 2 * 1024)
                .childHandler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline().addLast(new TestReplayInboudHandler());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(msg);
                                ReferenceCountUtil.release(msg);
                            }
                        });
                    }
                })
                .bind(8090)
                .sync();
        channelFuture.channel().closeFuture().sync();
    }

    @Test
    public void testReplayClient() throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(new NioEventLoopGroup());
        bootstrap.channel(NioSocketChannel.class)
                .handler(new ChannelInboundHandlerAdapter(){
            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {

            }
        });
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1",8090).addListener((ChannelFutureListener) ch ->{
            if(ch.isSuccess()){
                int length = 1024 * 5;
                byte[] sendMessage = new byte[length];

                ByteBuf byteBuf = ch.channel().alloc().buffer();
                byteBuf.writeInt(length);
                byteBuf.writeBytes(sendMessage);
                ch.channel().writeAndFlush(byteBuf);
            } else {
                System.out.println("un connection");
            }
        });
        channelFuture.channel().closeFuture().sync();
    }

    @Test
    public void testStdout() throws IOException {
        System.in.read();
    }

    public static void main(String[] args) throws IOException {
        System.out.println(System.in.getClass());
    }
}
